package com.texplose.service.impl;

import com.texplose.entity.Recipe;
import com.texplose.entity.constant.SystemConstants;
import com.texplose.entity.vo.*;
import com.texplose.mapper.RecipeMapper;
import com.texplose.service.RecipeService;
import com.texplose.util.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : lyj
 * @Timer : 2022/5/18
 * @Description :
 */
@Service
public class RecipeServiceImpl implements RecipeService {
    @Autowired
    private RecipeMapper recipeMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public List<IndexCardsVo> getIndexCards() {
        return recipeMapper.getIndexCards();
    }

    @Override
    public List<IndexCardsVo> getIndexCardsByCreate() {
        return recipeMapper.getIndexCardsByCreate();
    }

    @Override
    public List<IndexCardsVo> getIndexCardsByLikes() {
        return recipeMapper.getIndexCardsByLikes();
    }

    @Override
    public ResponseResult getSwiper() {
        List<SwiperVo> swiperVos = recipeMapper.getSwiper();
        return ResponseResult.okResult(swiperVos);
    }

    @Override
    public ResponseResult getWorksByUserId(int userId) {
        List<UserWorksVo> userWorksVos = recipeMapper.getWorksByUserId(userId);

        return ResponseResult.okResult(userWorksVos);
    }

    @Override
    public ResponseResult getRecipeListByCategoryId(Integer id) {
        List<Recipe> recipeList = recipeMapper.getRecipeListByCategoryId(id);

        return ResponseResult.okResult(recipeList);
    }

    @Override
    public List<UserCollectionVo> getCollectionsByUserId(Integer id) {
        List<UserCollectionVo> userCollectionVos = recipeMapper.getCollectionsByUserId(id);

        return userCollectionVos;
    }

    @Override
    public void deleteRecipeById(Integer id) {
        recipeMapper.deleteRecipeById(id);
    }

    @Override
    public RecipeDetailVo getRecipeDetailById(Integer id) {
        RecipeDetailVo recipeDetailVo = recipeMapper.getRecipeDetailById(id);
        //直接从redis中拿view
        Integer view = redisCache.getCacheMapValue(SystemConstants.REDIS__VIEWS_MAP, id.toString());
        recipeDetailVo.setViews(view);
        return recipeDetailVo;
    }

    @Override
    public List<IndexCardsVo> searchRecipeByTitle(String title) {
        return recipeMapper.searchRecipeByTitle(title);
    }

    @Override
    public ResponseResult updateView(Integer id) {

        redisCache.incrementCacheMapValue(SystemConstants.REDIS__VIEWS_MAP, id.toString(), 1);

        return ResponseResult.okResult();
    }

    @Override
    public void updateViewById(int id, int views) {
        recipeMapper.updateViewById(id, views);
    }

    @Override
    public ResponseResult addRecipe(AddRecipeVo addRecipeVo) {
        recipeMapper.addRecipe(addRecipeVo);
        return ResponseResult.okResult();
    }

    @Override
    public Integer countByUserId(Integer id) {
        Integer count = recipeMapper.countByUserId(id);
        if (count==null || count==0){
            count = 0;
        }
        return count;
    }
}
